package com.yunquan.ohana.ui.selectFrames.decoration

import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.Rect
import android.view.View
import androidx.core.content.res.ResourcesCompat
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.yunquan.ohana.R
import com.yunquan.ohana.ui.selectFrames.adapter.SelectFramesAdapter
import com.yunquan.ohana.utils.Utils

class FramesHeaderDecoration(val context: Context) : RecyclerView.ItemDecoration() {
    private var currentHeaderView: View? = null
    private val paint: Paint = Paint(Paint.ANTI_ALIAS_FLAG)
    private val textPaint = Paint(Paint.ANTI_ALIAS_FLAG)
    private val textRect = Rect()
    private val textPaddingLeft = Utils.instance.dp2px(28f)
    private var mItemHeaderHeight = Utils.instance.dp2px(32f)

    init {
        paint.color = context.getColor(R.color.background)
        textPaint.color = context.getColor(R.color.c_54)
        textPaint.textSize = Utils.instance.sp2px(12f).toFloat()
        val typeFace = ResourcesCompat.getFont(context, R.font.montserrat_regular)
        textPaint.typeface = typeFace
    }

    override fun onDraw(c: Canvas, parent: RecyclerView, state: RecyclerView.State) {
        super.onDraw(c, parent, state)
        if (parent.adapter is SelectFramesAdapter) {
            val adapter = parent.adapter as SelectFramesAdapter
            val childCount = parent.childCount
            for (i in 0 until childCount) {
                val child = parent.getChildAt(i)
                val position = parent.getChildAdapterPosition(child)
                val isHeader = adapter.isGroupHeader(position)
                val left = parent.paddingLeft
                val right = parent.width - parent.paddingRight
                if (isHeader) {
                    c.drawRect(left.toFloat(), (child.top - mItemHeaderHeight).toFloat(), right.toFloat(), child.top.toFloat(), paint)
                    val online = adapter.getItemData(position).online ?: false
                    val text = if (online) {
                        textPaint.color = context.getColor(R.color.c_54)
                        parent.context.getString(R.string.online)
                    } else {
                        textPaint.color = context.getColor(R.color.c_ffff3535)
                        parent.context.getString(R.string.offline)
                    }
                    textPaint.getTextBounds(text, 0, text.length, textRect)
                    c.drawText(
                        text,
                        left.toFloat() + textPaddingLeft,
                        (child.top - mItemHeaderHeight / 2 + textRect.height() / 2).toFloat(),
                        textPaint
                    )
                }
            }
        }
    }

    override fun onDrawOver(c: Canvas, parent: RecyclerView, state: RecyclerView.State) {
        super.onDrawOver(c, parent, state)
        val childCount = parent.childCount
        if (childCount == 0) return

        if (parent.adapter is SelectFramesAdapter) {
            val adapter = parent.adapter as SelectFramesAdapter
            val position = (parent.layoutManager as LinearLayoutManager).findFirstVisibleItemPosition()
            val view = parent.findViewHolderForAdapterPosition(position)?.itemView
            val isHeader = adapter.isGroupHeader(position + 1)
            val top = parent.paddingTop
            val left = parent.paddingLeft
            val right = parent.width - parent.paddingRight
            if (isHeader) {
                val bottom = mItemHeaderHeight.coerceAtMost(view!!.bottom)
                c.drawRect(
                    left.toFloat(),
                    (top + view.top - mItemHeaderHeight).toFloat(),
                    right.toFloat(),
                    (top + bottom).toFloat(),
                    paint
                )
                val online = adapter.getItemData(position).online ?: false
                val text = if (online) {
                    textPaint.color = context.getColor(R.color.c_54)
                    parent.context.getString(R.string.online)
                } else {
                    textPaint.color = context.getColor(R.color.c_ffff3535)
                    parent.context.getString(R.string.offline)
                }
                textPaint.getTextBounds(text, 0, text.length, textRect)
                c.drawText(
                    text,
                    left.toFloat() + textPaddingLeft,
                    (top + mItemHeaderHeight / 2 + textRect.height() / 2 - (mItemHeaderHeight - bottom)).toFloat(),
                    textPaint
                )
            } else {
                c.drawRect(
                    left.toFloat(),
                    top.toFloat(),
                    right.toFloat(),
                    (top + mItemHeaderHeight).toFloat(),
                    paint
                )
                val online = adapter.getItemData(position).online ?: false
                val text = if (online) {
                    textPaint.color = context.getColor(R.color.c_54)
                    parent.context.getString(R.string.online)
                } else {
                    textPaint.color = context.getColor(R.color.c_ffff3535)
                    parent.context.getString(R.string.offline)
                }
                textPaint.getTextBounds(text, 0, text.length, textRect)
                c.drawText(
                    text,
                    left.toFloat() + textPaddingLeft,
                    (top + mItemHeaderHeight / 2 + textRect.height() / 2).toFloat(),
                    textPaint
                )
            }
            c.save()
        }
    }

    override fun getItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State) {
        super.getItemOffsets(outRect, view, parent, state)
        if (parent.adapter is SelectFramesAdapter) {
            val adapter = parent.adapter as SelectFramesAdapter
            val position = parent.getChildLayoutPosition(view)
            val isHeader = adapter.isGroupHeader(position)
            if (isHeader) {
                outRect.top = mItemHeaderHeight
            }
        }
    }
}